package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;

import view.BorrowBookPage;
import view.ViewBookPage;
import dto.BookDTO;
import dto.BorrowBookDTO;
import dto.LogonDTO;

public class ViewBookController {

	private ViewBookPage viewBook;
	private CommonBookController commonController;

	void viewBook(LogonDTO logonDTO, BookDTO bookDTO) {
		viewBook = new ViewBookPage(logonDTO, bookDTO);

		viewBook.setModal(true);
		viewBook.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		viewBook.setVisible(true);

	}

	class ViewBookListener implements ActionListener {
		private BorrowBookPage borrowBook;
		private BorrowBookDTO borrowDTO;

		ViewBookListener(BorrowBookPage borrowBook) {
			this.borrowBook = borrowBook;
		}

		public void actionPerformed(ActionEvent e) {
			borrowDTO = borrowBook.getBorrowBookDTO();
			String ddc = borrowDTO.getBookDTO().getDeweyDecimalNo();

			BookDTO bookDTO = commonController.populateBookDTO(ddc);

			if (bookDTO != null) {
				viewBook(borrowBook.getLogonDTO(), bookDTO);

			} else {
				borrowBook.displayMessage("Catalog Number (DDC) unrecognized: "
						+ ddc);
			}
		}
	}
}
